package com.yuelao.rabbitmq.queue;


import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * @author FLY
 * @version v1.0
 * @project yuelao-server
 * @date 2017/9/15 14:30
 * @修改记录 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 */
@Configuration
public class RabbitmqConfig {
    /**
     * 简单队列应用可以不用绑定转换器,可以指定多个消费者
     * @return
     */
    @Autowired
    private CachingConnectionFactory cachingConnectionFactory;

    @Bean
    public Queue simpleQueue() {
        return new Queue("simpleQueue");
    }

    @Bean
    public Queue topic() {
        return new Queue("topic.message");
    }

    @Bean
    public Queue topics() {
        return new Queue("topic.messages");
    }

    @Bean
    public Queue topicA() {
        return new Queue("topicA");
    }

    @Bean
    public Queue topicB() {
        return new Queue("topicB");
    }

    @Bean
    public Queue topicC() {
        return new Queue("topicC");
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("exchange");
    }

    /**
     * 主题，订阅转发器，绑定此类转发器的队列都会接收到消息
     * @return
     */
    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    /**
     * 绑定的队列，完全匹配
     * @param topic
     * @param exchange
     * @return
     */
    @Bean
    Binding bindExchangeMessage(Queue topic, TopicExchange exchange) {
        return BindingBuilder.bind(topic).to(exchange).with("topic.message");
    }

    /**
     * 被绑定的队列将会模糊匹配
     * @param topics
     * @param exchange
     * @return
     */
    @Bean
    Binding bindExchangeMessages(Queue topics, TopicExchange exchange) {
        return BindingBuilder.bind(topics).to(exchange).with("topic.#");
    }

    /**
     * 消息对垒订阅模式 rout_key可以为任意值
     * @param topicA
     * @param fanoutExchange
     * @return
     */
    @Bean
    Binding bindExchangeA(Queue topicA, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(topicA).to(fanoutExchange);
    }

    @Bean
    Binding bindExchangeB(Queue topicB, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(topicB).to(fanoutExchange);
    }

    @Bean
    Binding bindExchangeC(Queue topicC, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(topicC).to(fanoutExchange);
    }

    @Bean
    /** 因为要设置回调类，所以应是prototype类型，如果是singleton类型，则回调类为最后一次设置 */
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(cachingConnectionFactory);
        return template;
    }
}
